package govenor.status;

import govenor.manager.GovenorManager;
import govenor.overwatch.ClientDescriptor;

import java.io.StringWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import model.jena.model.GovenorModelBuilder;

import com.hp.hpl.jena.rdf.model.Model;

public class ClientStatusHandler implements Runnable{

	private GovenorManager manager;
	private Map<String, ClientDescriptor> mapOfClients;
	
	ClientStatusHandler(GovenorManager manager){
		this.manager = manager;
	}
	public void run() {
		System.out.println(this.getClass().getName()+"Scheduler calling");
		this.mapOfClients = this.manager.getClientMap();
		Set<String> setOfHostsnames = this.mapOfClients.keySet();
		Iterator<String> iter = setOfHostsnames.iterator();
		while(iter.hasNext()){
			String key = iter.next();
			ClientDescriptor cd = this.mapOfClients.get(key) ;
			StringWriter writer = new StringWriter();
			Model model = GovenorModelBuilder.buildModel(this.manager.buildMapOfFilesToHosts());
			model.write(writer);
			model.write(System.out);
			String[] params = new String[1];
			params[0] = writer.toString();
			System.out.println(params);
			this.manager.sendClientsUpdate(cd.getHostname(), ""+cd.getCommPort(), params);
		}
		System.out.println(this.getClass().getName()+" : Done");
	}

}
